package com.qa.utils;

import com.qa.entity.LoginUser;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author 徐
 */
public class UserUtil {
    /**
     * 获取当前登录的用户的id
     * @return Long用户id
     */
    public static Long getCurrentUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
            return loginUser.getUser().getId();
        }
        return null;
    }
}
